package com.food.user.consumer;

import com.food.user.dto.CartReturnDto;
import com.food.user.dto.DeleteCartDto;
import com.food.user.dto.UserCartDto;
import com.food.user.util.JwtUtil;
import com.food.user.util.RedisKeyUtil;
import com.food.user.util.RedisUtil;
import com.food.user.util.WebUtil;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(consumerGroup = "deleteCart",topic = "delete_cart")
public class DeleteCart implements RocketMQListener<DeleteCartDto> {
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public void onMessage(DeleteCartDto dto) {

        for (UserCartDto userCartDto : dto.getCartReturnDto().getUserCartDtos()) {
            try {
                redisUtil.sRem(RedisKeyUtil.userCartKey(dto.getId()),userCartDto.getKey());
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            redisUtil.delete(userCartDto.getKey());
        }
    }
}
